/**
 * Created by liubin on 2017/3/2.
 */



var WhmjTableResultLayer = GameCCSBase.extend({
    Image_results : null,
    Image_dealCard : null,
    Image_huangzhuang : null,
    AtlasLabel_scores : null,
    Image_socres : null,

    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/WhmjTableResult.json");

        this.loadUI();

        mo.gameHelper.fixIosText(this.ccsNode);
    },

    reset : function(){
        for (var i = 0; i < 4; i++){
            this.Image_results[i].setVisible(false);
            this.Image_results[i].stopAllActions();
        }

        this.Image_huangzhuang.setVisible(false);
        this.Image_huangzhuang.stopAllActions();
    },

    loadUI : function(){
        this.Image_results = [];
        for(var i = 0; i < 4; i++){
            var Image_result = mo.seekWidgetByName(this.ccsNode, "Image_result" + i);
            this.Image_results.push(Image_result);

            Image_result.setVisible(false);
        }

        this.Image_dealCard = [];
        for(var i = 0; i < 4; i++){
            var Image_dealCard = mo.seekWidgetByName(this.ccsNode, "Image_dealCard" + i);
            this.Image_dealCard.push(Image_dealCard);

            Image_dealCard.setVisible(false);

            var card = new WhmjCard();
            Image_dealCard.addChild(card);
        }

        this.Image_huangzhuang = mo.seekWidgetByName(this.ccsNode, "Image_huangzhuang");
        this.Image_huangzhuang.setVisible(false);

        // this.Image_socres = [];
        // this.AtlasLabel_scores = [];
        // for (var i = 0; i < 3; i++){
        //     var Image_socre = mo.seekWidgetByName(this.ccsNode, "Image_socre" + i);
        //     this.Image_socres.push(Image_socre);
        //     Image_socre.setVisible(false);
        //
        //     var AtlasLabel_score = mo.seekWidgetByName(Image_socre, "AtlasLabel_score" + i);
        //     this.AtlasLabel_scores.push(AtlasLabel_score);
        //
        // }
    },

    //-1 不存在
    //1 吃
    //2 碰
    //4 明杠
    //5 暗杠
    //6 补杠
    //3 胡
    //33 自摸
    showResult : function(results){
        for (var i = 0; i < 4; i++){
            if (results[i] == -1){
                this.Image_results[i].setVisible(false);
                this.Image_results[i].stopAllActions();
            }else{
                mo.gameHelper.loadTextureImage(this.Image_results[i], this.getResultFile(results[i]));
                var Image_op = mo.seekWidgetByName(this.Image_results[i], "Image_op");
                mo.gameHelper.loadTextureImage(Image_op, this.getResultFile(results[i]));
                var Image_light = mo.seekWidgetByName(this.Image_results[i], "Image_light");
                mo.gameHelper.loadTextureImage(Image_light, this.getResultLightFile(results[i]));

                this.Image_results[i].stopAllActions();
                this.Image_results[i].setVisible(true);
                this.Image_results[i].setOpacity(255);
                this.Image_results[i].setScale(0.8);

                var time0 = 0.6;
                var time1 = 0.2;
                var time2 = 0.4;
                var time3 = 0.1;
                if (results[i] == 3 || results[i] == 33){
                    time3 = 1.5;
                }

                this.Image_results[i].runAction(cc.sequence(
                    cc.scaleTo(time0, 1.2)
                ));
                this.Image_results[i].runAction(cc.sequence(
                    cc.delayTime(time1),
                    cc.delayTime(time3),
                    cc.fadeOut(time2),
                    cc.hide()
                ));

                Image_light.stopAllActions();
                var parentSize = this.Image_results[i].getContentSize();
                Image_light.setPosition(cc.p(parentSize.width / 2, parentSize.height / 2));
                Image_light.setVisible(true);
                Image_light.setOpacity(255);
                Image_light.setScale(1);

                Image_light.runAction(cc.sequence(
                    cc.scaleTo(time2, 1.2),
                    cc.fadeOut(time1)
                ));
            }
        }
    },

    getResultFile : function(result){
        var file = "res/ccs/whmj/table/effect/peng2.png";
        if (result == 1){
            file = "res/ccs/whmj/table/effect/chi2.png";
        }else if (result == 2){
            file = "res/ccs/whmj/table/effect/peng2.png";
        }else if (result == 4 || result == 5 || result == 6 || result == 7){
            file = "res/ccs/whmj/table/effect/gang2.png";
        }else if (result == 3){
            file = "res/ccs/whmj/table/effect/hu2.png";
        }else if (result == 33){
            file = "res/ccs/whmj/table/effect/hu2.png";
        }
        return file;
    },

    getResultLightFile : function(result){
        var file = "res/ccs/whmj/table/effect/peng1.png";
        if (result == 1){
            file = "res/ccs/whmj/table/effect/chi1.png";
        }else if (result == 2){
            file = "res/ccs/whmj/table/effect/peng1.png";
        }else if (result == 4 || result == 5 || result == 6 || result == 7){
            file = "res/ccs/whmj/table/effect/gang1.png";
        }else if (result == 3){
            file = "res/ccs/whmj/table/effect/hu2.2.png";
        }else if (result == 33){
            file = "res/ccs/whmj/table/effect/hu2.png";
        }
        return file;
    },

    //0 不显示
    showDealCard : function(cardNums){
        for(var i = 1; i < 4; i++){
            if (cardNums[i] == 0){
                this.Image_dealCard[i].stopAllActions();
                this.Image_dealCard[i].setVisible(false);
            }else{
                this.Image_dealCard[i].stopAllActions();
                this.Image_dealCard[i].setVisible(true);
                this.Image_dealCard[i].runAction(cc.sequence(cc.delayTime(1), cc.hide()));

                var card = this.Image_dealCard[i].getChildren()[0];
                card.setCardNum(cardNums[i]);
                card.setCardKind(0, 3);
                card.setAnchorPoint(cc.p(0.5, 0.5));
                card.setPosition(cc.p(this.Image_dealCard[i].getContentSize().width / 2, this.Image_dealCard[i].getContentSize().height / 2));
            }
        }
    },

    //显示黄庄
    showHuangzhuang : function(){
        this.Image_huangzhuang.stopAllActions();
        this.Image_huangzhuang.setVisible(true);
        this.Image_huangzhuang.setScale(0.5);

        this.Image_huangzhuang.runAction(cc.sequence(
            cc.scaleTo(0.2, 1.2),
            cc.scaleTo(0.1, 1.0),
            cc.delayTime(1.5),
            cc.hide()
        ))
    },

    //显示分数
    setResultScore : function(scores){
        // var pos = [
        //     cc.p(230, 224.5),
        //     cc.p(mo.gameSize.x - 210, mo.gameSize.y - 225),
        //     cc.p(230, mo.gameSize.y - 225)
        // ];
        //
        // var anchorPos = [
        //     cc.p(0, 0.5),
        //     cc.p(1, 0.5),
        //     cc.p(0, 0.5)
        // ];
        //
        // for (var i = 0; i < scores.length; i++){
        //     this.AtlasLabel_scores[i].stopAllActions();
        //     if (scores[i] != 0){
        //         this.Image_socres[i].setVisible(true);
        //         this.Image_socres[i].setOpacity(0);
        //         this.Image_socres[i].setPosition(pos[i]);
        //         this.Image_socres[i].runAction(cc.sequence(
        //             cc.fadeIn(0.2),
        //             cc.delayTime(1.5),
        //             cc.spawn(cc.fadeOut(0.3), cc.moveBy(0.3, cc.p(0, 50))),
        //             cc.hide()
        //         ));
        //
        //         var showStr = "./" + scores[i];
        //         if (scores[i] > 0){
        //             this.AtlasLabel_scores[i].setProperty(showStr, "res/ccs/whmj/table/result/piaofen_add.png", 42, 53, ".");
        //         }else{
        //             this.AtlasLabel_scores[i].setProperty(showStr, "res/ccs/whmj/table/result/piaofen_sub.png", 42, 53, ".");
        //         }
        //     }else{
        //         this.Image_socres[i].setVisible(false);
        //     }
        // }
    }
});